package com.longtop.perform.kh.score.web;

import java.util.HashMap;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.kh.score.service.ScoreService;
import com.longtop.perform.kh.score.vo.ScoreParamVO;
import com.longtop.perform.khyg.model.TKhygIndex;

/**
 * 
 * @author 孙景弘
 * 
 */
public class ScoreMainAction extends BaseAction {

	private ScoreService service;
	private final String DATASET_PORJECT_INDEX = "Dataset_project_index";// 指标体系-对应指标
	private final String DATASET_SCOREMODEL = "Dataset_scoreModel";// 评分模型
	private final String DATASET_SCOREPARAM = "Dataset_scoreParam";// 评分模型-函数参数

	// 查询条件：
	Map<String, Object> queryMap = new HashMap<String, Object>();

	public ScoreMainAction() {
		this.service = this.getBean(ScoreService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		Dataset ds_projectIndex = this.getDataset(context, DATASET_PORJECT_INDEX);
		Dataset ds_score = this.getDataset(context, DATASET_SCOREMODEL);
		if (!WebUtil.validateDatasetExist(this, ds_projectIndex, DATASET_PORJECT_INDEX) || !WebUtil.validateDatasetExist(this, ds_score, DATASET_SCOREMODEL))
			return;
		// 方案-指标：
		String projectId = this.getParameter(context, "projectId");
		queryMap.put("projectId", projectId);
		queryMap.put("indexType", "1");// 1代表定性指标，这里不对定性指标进行设置评分规则。排除定性指标
		if (!StringUtil.isEmpty(projectId)) {
			service.queryForPageByIbatis("khScoreSql.queryProjectIndex", null, queryMap, ds_projectIndex, false);
		}
		// 评分模型：
		ds_score = service.getScoreModelList(ds_score);
	}

	/**
	 * 装载评分模型/初始化数据
	 * 
	 * @param context
	 */
	public void loadScore(IActionContext context) {
		Dataset ds_scoreParam = this.getDataset(context, DATASET_SCOREPARAM);
		Dataset ds_projectIndex = this.getDataset(context, DATASET_PORJECT_INDEX);
		if (!WebUtil.validateDatasetExist(this, ds_scoreParam, DATASET_SCOREPARAM))
			return;
		try {
			ScoreParamVO paramVO = service.loadScore(ds_projectIndex);
			if (paramVO != null) {
				ds_scoreParam.clear();
				ds_scoreParam.appendObject(paramVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 保存评分模型
	 * 
	 * @param context
	 */
	public void saveScore(IActionContext context) {
		Dataset ds_projectIndex = this.getDataset(context, DATASET_PORJECT_INDEX);
		Dataset ds_scoreParam = this.getDataset(context, DATASET_SCOREPARAM);
		if (!WebUtil.validateDatasetChange(this, ds_scoreParam, DATASET_SCOREPARAM))// 判断是DATASET是否有改变
			return;
		try {
			TKhygIndex projectIndex = service.saveScore(ds_projectIndex, ds_scoreParam);
			ds_projectIndex.setValue("formula", projectIndex.getFormula());
			this.hintInfoMessage("保存成功。");
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("保存失败：" + e.getMessage());
		}

	}

}
